{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 支持向量机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from time import time\n",
    "import mnist_loader \n",
    "from sklearn import svm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "training time 750.811 s\n"
     ]
    }
   ],
   "source": [
    "training_data, validation_data, test_data = mnist_loader.load_data()\n",
    "# train\n",
    "\n",
    "t0 = time()\n",
    "clf = svm.SVC(kernel='rbf')\n",
    "clf.fit(training_data[0], training_data[1])\n",
    "print(\"training time %.3f s\" % round(time() - t0, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "testing time 277.675 s\n",
      "\n",
      "9435 of 10000 values correct.\n"
     ]
    }
   ],
   "source": [
    "# test\n",
    "t0 = time()\n",
    "predictions = [int(a) for a in clf.predict(test_data[0])]\n",
    "num_correct = sum(int(a == y) for a, y in zip(predictions, test_data[1]))\n",
    "print(\"testing time %.3f s\\n\" % round(time() - t0, 3))\n",
    "\n",
    "print(\"%s of %s values correct.\" % (num_correct, len(test_data[1])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "training time 532.060 s\n"
     ]
    }
   ],
   "source": [
    "t0 = time()\n",
    "clf = svm.SVC(kernel='linear')\n",
    "clf.fit(training_data[0], training_data[1])\n",
    "print(\"training time %.3f s\" % round(time() - t0, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "testing time 155.485 s\n",
      "\n",
      "9390 of 10000 values correct.\n"
     ]
    }
   ],
   "source": [
    "# test\n",
    "t0 = time()\n",
    "predictions = [int(a) for a in clf.predict(test_data[0])]\n",
    "num_correct = sum(int(a == y) for a, y in zip(predictions, test_data[1]))\n",
    "print(\"testing time %.3f s\\n\" % round(time() - t0, 3))\n",
    "\n",
    "print(\"%s of %s values correct.\" % (num_correct, len(test_data[1])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
